<%@ page import="java.security.SecureRandom" %>
<%@ page import="java.util.Base64" %>
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<!DOCTYPE html>
<html lang="zh-CN">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>选课系统登录界面</title>
    <style>
        body {
            font-family: Arial, sans-serif;
            display: flex;
            justify-content: center;
            align-items: center;
            height: 100vh;
            margin: 0;
            background-color: #f7f7f7;
        }
        .login-container {
            background-color: white;
            padding: 20px;
            border-radius: 5px;
            box-shadow: 0 0 10px rgba(0, 0, 0, 0.1);
            width: 300px;
        }
        .login-container h2 {
            text-align: center;
            margin-bottom: 20px;
        }
        .form-group {
            margin-bottom: 15px;
        }
        .form-group label {
            display: block;
            margin-bottom: 5px;
        }
        .form-group input {
            width: 100%;
            padding: 8px;
            box-sizing: border-box;
            border: 1px solid #ccc;
            border-radius: 3px;
        }
        .captcha-container {
            display: flex;
            align-items: center;
        }
        .captcha-container input {
            width: 60%;
            margin-right: 10px;
        }
        .captcha-container img {
            width: 40%;
            height: 36px;
            cursor: pointer;
        }
        .third-party-login {
            text-align: center;
        }
        .third-party-login button {
            margin: 5px;
            padding: 8px 15px;
            border: none;
            border-radius: 3px;
            cursor: pointer;
        }
        .third-party-login .wechat {
            background-color: #1aad19;
            color: white;
        }
        .third-party-login .qq {
            background-color: #4a90e2;
            color: white;
        }
        .form-group button {
            width: 100%;
            padding: 10px;
            background-color: #007BFF;
            color: white;
            border: none;
            border-radius: 3px;
            cursor: pointer;
        }
        .form-group button:hover {
            background-color: #0056b3;
        }
        .register-link {
            text-align: center;
            margin-top: 15px;
        }
        .register-link a {
            color: #007BFF;
            text-decoration: none;
        }
        .register-link a:hover {
            text-decoration: underline;
        }
    </style>
</head>
<body>
<div class="login-container">
    <h2>选课系统登录</h2>
    <form action="login" method="post">
        <div class="form-group">
            <label for="username">账号</label>
            <input type="text" id="username" name="username" required>
        </div>
        <div class="form-group">
            <label for="password">密码</label>
            <input type="password" id="password" name="password" required>
        </div>
        <div class="form-group captcha-container">
            <%
                // 生成验证码并存入request属性
                final SecureRandom random = new SecureRandom();
                byte[] bytes = new byte[4];
                random.nextBytes(bytes);
                String captcha = Base64.getUrlEncoder().withoutPadding().encodeToString(bytes).substring(0, 4).toUpperCase();
            %>
            <input type="text" id="captcha" name="captcha" placeholder="验证码" required>
            <input type="hidden" name="serverCaptcha" value="<%=captcha%>">
            <img src="https://dummyimage.com/100x36/000/fff&text=<%=captcha%>" alt="验证码" onclick="refreshCaptcha()">
        </div>
        <div class="form-group">
            <button type="submit">登录</button>
        </div>
        <% if (request.getAttribute("error") != null) { %>
        <div style="color:red;"><%= request.getAttribute("error") %></div>
        <% } %>
    </form>
    <div class="third-party-login">
        <button class="wechat">微信登录</button>
        <button class="qq">QQ登录</button>
    </div>
    <div class="register-link">
        <a href="register.jsp">没有账号？点击注册</a>
    </div>
</div>

<script>
    function refreshCaptcha() {
        // 使用fetch API获取新验证码
        fetch('refreshCaptcha')
            .then(response => response.text())
            .then(captcha => {
                const captchaImg = document.querySelector('.captcha-container img');
                captchaImg.src = 'https://dummyimage.com/100x36/000/fff&text=' + captcha;
                // 将新验证码存入隐藏字段（可选）
                document.getElementById('captchaValue').value = captcha;
            });
    }
</script>
</body>
</html>
